import axios from 'axios'

// 创建axios 实例
const service = axios.create({
    baseURL: process.env.VUE_APP_URL,
    // timeout: 10000
})

// 请求拦截
service.interceptors.request.use(
    config => {
        //------------解决方法--------------------------------------------------
        config.headers['Content-Type'] = 'application/x-www-form-urlencoded' 

        // config.headers["Token"] = tokenUtil.getToken();
        return config
    },
    error => {
        console.log(error)
        Promise.reject(error)
    }
)

// 响应拦截
service.interceptors.response.use(
    response => {
        return response.data
    },
    error => {
        console.error(error.toString())

        const res = error.response;
        if (typeof res == "undefined") {
            return Promise.reject(error)
        }
        if (res.status == 500) {
            alert(error.toString())
            return;
        }
        if (res.status == 401) {
            setTimeout(() => {
                // login.wxLogin(false, location.pathname + location.search);
                console.log(401);
            }, 1000);
        }
        return Promise.reject(error)
        // else{
        //   return Promise.reject(error)
        // }
    }
)

export default service